using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Data;
using TexasHoldEmCroupier.Views;
using TexasHoldEmCroupier.Enumerators;
using TexasHoldEmCroupier.UserControls;

namespace TexasHoldEmCroupier.ApplicationController
{
    public partial class Controller
    {
        #region Methods
        private void PlayerDetailsWizardView_Show(
            int thePlayerIndex)
        {
            PlayerDetailsWizardView theView =
                new PlayerDetailsWizardView(thePlayerIndex);

            theView.buttonPrevious.Click += 
                new EventHandler(PlayerDetailsWizardView_buttonPrevious_Click);
            theView.buttonNext.Click += 
                new EventHandler(PlayerDetailsWizardView_buttonNext_Click);
            theView.buttonExit.Click += 
                new EventHandler(PlayerDetailsWizardView_buttonExit_Click);

            theView.DialogResult = 
                DialogResult.None;
            theView.PlayerName =
                this._MainView.GetPlayerStrip(thePlayerIndex).PlayerName;
            theView.Account =
                this._MainView.GetPlayerStrip(thePlayerIndex).Account;

            while (String.IsNullOrEmpty(theView.PlayerName) &&
                !theView.IsDisposed)
            {
                theView.ShowDialog(this._MainView);
            }
        }

        #region Event Handlers
        private void PlayerDetailsWizardView_buttonExit_Click(
            object sender, 
            EventArgs e)
        {
            Button theExitButton =
                sender as Button;
            PlayerDetailsWizardView theParentView =
                theExitButton.FindForm() as PlayerDetailsWizardView;

            switch (MessageBox.Show(
                   this._MainView,
                   "Are you sure you want to exit the game?",
                   this._MainView.Text,
                   MessageBoxButtons.YesNo,
                   MessageBoxIcon.Question,
                   MessageBoxDefaultButton.Button2))
            {
                case DialogResult.Yes:
                    this._MainView.DialogResult = DialogResult.Abort;
                    this._MainView.Close();
                    return;
            } 
        }

        private void PlayerDetailsWizardView_buttonPrevious_Click(
            object sender, 
            EventArgs e)
        {
            Button thePreviousButton =
                sender as Button;
            PlayerDetailsWizardView theParentView =
                thePreviousButton.FindForm() as PlayerDetailsWizardView;

            this._MainView.GetPlayerStrip(theParentView.Index).Name =
                theParentView.PlayerName;
            this._MainView.GetPlayerStrip(theParentView.Index).Account =
                theParentView.Account;

            if (theParentView.Index.Equals(0))
            {
                theParentView.Hide();

                this.NumberOfPlayersWizardView_Show(
                    this._MainView.NumberOfPlayers);
            }
            else
            {
                theParentView.Hide();

                this.PlayerDetailsWizardView_Show(
                    --theParentView.Index);
            }
        }

        private void PlayerDetailsWizardView_buttonNext_Click(
            object sender, 
            EventArgs e)
        {
            Button theNextButton =
                sender as Button;
            PlayerDetailsWizardView theParentView =
                theNextButton.FindForm() as PlayerDetailsWizardView;

            if (theParentView.IsFormValid())
            {
                this._MainView.GetPlayerStrip(theParentView.Index).PlayerName =
                    theParentView.PlayerName;
                this._MainView.GetPlayerStrip(theParentView.Index).Account =
                    theParentView.Account;

                if (theParentView.Index.Equals(this._MainView.NumberOfPlayers - 1))
                {
                    theParentView.Hide();

                    Application.DoEvents();

                    this.AddCommentaryLine(
                        MessageType.Information,
                        String.Format(
                            CultureInfo.CurrentCulture,
                            "There are {0} players",
                            this._MainView.NumberOfPlayers));
                }
                else
                {
                    theParentView.Hide();

                    this.PlayerDetailsWizardView_Show(
                        ++theParentView.Index);
                }
            }
        }
        #endregion
        #endregion
    }
}
